package com.ustarlink.live.channel;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by cfm on 16-1-15.
 */
public class ChannelXPPAndSQLite {

    private static final String KEY_CHANNEL = "CHANNEL";
    private static final String KEY_CATEGORY = "package";
    private static String myTag = "uStarLink Live";
    public static void writeToDatabase(Context context){

        ChannelOpenHelper channelOpenHelper;
        Channel curChannel = null;
        int counter = 0;
        /** 在解析时text临时变量 **/
        String curText = "";
        try {
            channelOpenHelper = new ChannelOpenHelper(context);
            SQLiteDatabase db = channelOpenHelper.getWritableDatabase();
            /** 获得factory和PullParser **/
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xpp = factory.newPullParser();

            /** 打开InputStream和读取XML文件 **/
            FileInputStream fis = context.openFileInput("tvs_epg.xml");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));


            /** 指向我们需要解析的文件 **/
            xpp.setInput(reader);
            Log.d(myTag, "真在解析");
            /** 获取初始化的eventType() **/

            /**循环直到END_DOCUMENT**/

            String tempCategory = "";
            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT){

                if (xpp.getEventType() ==XmlPullParser.START_TAG){
                    /**获取当前节点**/
                    String tagname = xpp.getName();
                    if (tagname.equals(KEY_CATEGORY)){
                        tempCategory = xpp.getAttributeValue(0);
                    }
                    if (tagname.equals(KEY_CHANNEL)){
                        counter ++;
                        // 如果是channel开始时new Channel
                        curChannel = new Channel();
                        curChannel.setId(xpp.getAttributeValue(0));
                        curChannel.setChannelName(xpp.getAttributeValue(2));
                        curChannel.setChannelUrl(xpp.getAttributeValue(3));
                        curChannel.setCategoryName(tempCategory);
                      //  Log.i(myTag, String.valueOf(counter) + "\t" + curChannel.toString());

                        ContentValues values = new ContentValues();
                        values.put(ChannelsTable.ChannelsEntry.COLUMN_NAME_ENTRY_ID, xpp.getAttributeValue(0));
                        values.put(ChannelsTable.ChannelsEntry.COLUMN_NAME_TITLE, xpp.getAttributeValue(2));
                        values.put(ChannelsTable.ChannelsEntry.COLUMN_URL, xpp.getAttributeValue(3));
                        values.put(ChannelsTable.ChannelsEntry.COLUMN_NAME_CATEGORY,tempCategory);
                        db.insert(ChannelsTable.ChannelsEntry.TABLE_NAME,
                                null,
                                values);
                        Log.i(myTag, "已写入数据库 " + String.valueOf(counter) + "\t" + curChannel.toString());
                    }
                }

                //下一次
                xpp.next();

            }



        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }

        catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(myTag, "已写入"+String.valueOf(counter)+ "条数据到数据库 ");

    }
}
